package com.toidep.activities;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.view.MotionEvent;

import com.toidep.R;
import com.toidep.base.BaseActivity;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;

public class SplashScreenActivity extends BaseActivity {

	protected boolean _active = true;
	protected int _splashTime = 1000;
	private Thread splashTread;
	private boolean isInit;

	int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		ConfigUtility.getConfig(SplashScreenActivity.this);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_splash_screen);
		init();
	}
	
	public void init() {
		
		isInit = true;
		
		if (!isNetworkAvailable()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(SplashScreenActivity.this);
			builder.setTitle(Constants.APP_NAME);
			builder.setIcon(R.drawable.ic_launcher);
			builder.setMessage(R.string.text_no_network);
			builder.setCancelable(true);
			builder.setNegativeButton(R.string.label_close, new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					showSplashScreen();
					dialog.dismiss();
				}
			});

			builder.setPositiveButton("Setting", new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					startActivity(new Intent(Settings.ACTION_SETTINGS));
				}
			});
			AlertDialog alert = builder.create();
			alert.setCancelable(true);
			alert.show();
		} else {
			showSplashScreen();
		}
		
		/*initImageFetcher(this, new InitImageFetcherListener() {

			@Override
			public void initSuccess() {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						// Detect ket noi mang
						if (!isNetworkAvailable()) {
							AlertDialog.Builder builder = new AlertDialog.Builder(SplashScreenActivity.this);
							builder.setTitle(Constants.APP_NAME);
							builder.setIcon(R.drawable.ic_launcher);
							builder.setMessage(R.string.text_no_network);
							builder.setCancelable(true);
							builder.setNegativeButton(R.string.label_close, new OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									showSplashScreen();
								}
							});

							builder.setPositiveButton("Setting", new OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									startActivity(new Intent(Settings.ACTION_SETTINGS));
								}
							});
							AlertDialog alert = builder.create();
							alert.setCancelable(true);
							alert.show();
						} else {
							showSplashScreen();
						}

					}
				});

			}

			@Override
			public void initFail() {
				finish();

			}
		});*/
	}

	@Override
	protected void onResume() {
		if (!isInit) {
			_splashTime = 500;
			showSplashScreen();
		}
		isInit = false;
		super.onResume();
	}

	private void showSplashScreen() {
		splashTread = new Thread() {
			@Override
			public void run() {
				try {
					int waited = 0;
					while (_active && (waited < _splashTime)) {
						sleep(100);
						if (_active) {
							waited += 100;
						}
					}
				} catch (InterruptedException e) {
					// do nothing
				} finally {
					finish();
					
					startActivity(new Intent(SplashScreenActivity.this.getBaseContext(), MainActivity.class));

					interrupt();
				}
			}
		};
		splashTread.start();
	}

	private boolean isNetworkAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
		return activeNetworkInfo != null;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				_active = false;
			}
		}
		return true;
	}

}
